package weaver.interfaces.action;

import com.alibaba.fastjson.JSONObject;
import com.engine.entity.UfDjfbjgVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @version 1.0
 * @Title ecology-9
 * @Company 泛微软件
 * @CreateDate 2023/12/21
 * @Description 绩效等级分布流程-考核登记重计算Action
 * @Author AdminZm
 */
public class GradeCountAgainAction implements Action {

    BaseBean baseBean = new BaseBean();

    @Override
    public String execute(RequestInfo requestInfo) {
        int billid = requestInfo.getRequestManager().getBillid();
        String requestId = requestInfo.getRequestid();
        String formtable = requestInfo.getRequestManager().getBillTableName();
        RecordSet rs = new RecordSet();
        RecordSet rs1 = new RecordSet();
        try {
            rs.executeQuery("SELECT * FROM " + formtable + " WHERE requestid = ?", requestId);
            if (rs.next()) {
                String szkhjh = rs.getString("szkhjh");
                String bosfgb = rs.getString("bosfgb");
                String opsfgb = rs.getString("opsfgb");
                String finsfgb = rs.getString("finsfgb");
                String iadsfgb = rs.getString("iadsfgb");
                String crisfgb = rs.getString("crisfgb");
                String site1aplusrs = rs.getString("site1aplusrs");
                if (StringUtils.isEmpty(site1aplusrs)) {
                    site1aplusrs = "0";
                }
                String site2aplusrs = rs.getString("site2aplusrs");
                if (StringUtils.isEmpty(site2aplusrs)) {
                    site2aplusrs = "0";
                }
                String site3aplusrs = rs.getString("site3aplusrs");
                if (StringUtils.isEmpty(site3aplusrs)) {
                    site3aplusrs = "0";
                }
                String site4aplusrs = rs.getString("site4aplusrs");
                if (StringUtils.isEmpty(site4aplusrs)) {
                    site4aplusrs = "0";
                }
                String site5aplusrs = rs.getString("site5aplusrs");
                if (StringUtils.isEmpty(site5aplusrs)) {
                    site5aplusrs = "0";
                }

                String site1ars = rs.getString("site1ars");
                if (StringUtils.isEmpty(site1ars)) {
                    site1ars = "0";
                }
                String site2ars = rs.getString("site2ars");
                if (StringUtils.isEmpty(site2ars)) {
                    site2ars = "0";
                }
                String site3ars = rs.getString("site3ars");
                if (StringUtils.isEmpty(site3ars)) {
                    site3ars = "0";
                }
                String site4ars = rs.getString("site4ars");
                if (StringUtils.isEmpty(site4ars)) {
                    site4ars = "0";
                }
                String site5ars = rs.getString("site5ars");
                if (StringUtils.isEmpty(site5ars)) {
                    site5ars = "0";
                }

                String site1brs = rs.getString("site1brs");
                if (StringUtils.isEmpty(site1brs)) {
                    site1brs = "0";
                }
                String site2brs = rs.getString("site2brs");
                if (StringUtils.isEmpty(site2brs)) {
                    site2brs = "0";
                }
                String site3brs = rs.getString("site3brs");
                if (StringUtils.isEmpty(site3brs)) {
                    site3brs = "0";
                }
                String site4brs = rs.getString("site4brs");
                if (StringUtils.isEmpty(site4brs)) {
                    site4brs = "0";
                }
                String site5brs = rs.getString("site5brs");
                if (StringUtils.isEmpty(site5brs)) {
                    site5brs = "0";
                }

                String site1crs = rs.getString("site1crs");
                if (StringUtils.isEmpty(site1crs)) {
                    site1crs = "0";
                }
                String site2crs = rs.getString("site2crs");
                if (StringUtils.isEmpty(site2crs)) {
                    site2crs = "0";
                }
                String site3crs = rs.getString("site3crs");
                if (StringUtils.isEmpty(site3crs)) {
                    site3crs = "0";
                }
                String site4crs = rs.getString("site4crs");
                if (StringUtils.isEmpty(site4crs)) {
                    site4crs = "0";
                }
                String site5crs = rs.getString("site5crs");
                if (StringUtils.isEmpty(site5crs)) {
                    site5crs = "0";
                }

                String site1drs = rs.getString("site1drs");
                if (StringUtils.isEmpty(site1drs)) {
                    site1drs = "0";
                }
                String site2drs = rs.getString("site2drs");
                if (StringUtils.isEmpty(site2drs)) {
                    site2drs = "0";
                }
                String site3drs = rs.getString("site3drs");
                if (StringUtils.isEmpty(site3drs)) {
                    site3drs = "0";
                }
                String site4drs = rs.getString("site4drs");
                if (StringUtils.isEmpty(site4drs)) {
                    site4drs = "0";
                }
                String site5drs = rs.getString("site5drs");
                if (StringUtils.isEmpty(site5drs)) {
                    site5drs = "0";
                }

                rs1.executeQuery("SELECT * FROM uf_djfbbl WHERE jxkhjh = ?", szkhjh);
                String aplusdbl = null;
                String aplusqzbs = null;
                String adbl = null;
                String aqzbs = null;
                String bdbl = null;
                String bqzbs = null;
                String cdbl = null;
                String cqzbs = null;
                String ddbl = null;
                String dqzbs = null;
                if (rs1.next()) {
                    aplusdbl = rs.getString("aplusdbl");
                    aplusqzbs = rs.getString("aplusqzbs");
                    if (StringUtils.isEmpty(aplusqzbs)) {
                        aplusqzbs = "0";
                    }
                    adbl = rs.getString("adbl");
                    aqzbs = rs.getString("aqzbs");
                    if (StringUtils.isEmpty(aqzbs)) {
                        aqzbs = "0";
                    }
                    bdbl = rs.getString("bdbl");
                    bqzbs = rs.getString("bqzbs");
                    if (StringUtils.isEmpty(bqzbs)) {
                        bqzbs = "0";
                    }
                    cdbl = rs.getString("cdbl");
                    cqzbs = rs.getString("cqzbs");
                    if (StringUtils.isEmpty(cqzbs)) {
                        cqzbs = "0";
                    }
                    ddbl = rs.getString("ddbl");
                    dqzbs = rs.getString("dqzbs");
                    if (StringUtils.isEmpty(dqzbs)) {
                        dqzbs = "0";
                    }
                } else {
                    requestInfo.getRequestManager().setMessage("等级分布比例获取失败，请检查！");
                    return Action.FAILURE_AND_CONTINUE;
                }
                BigDecimal aplusdblD = new BigDecimal(aplusdbl);
                BigDecimal adblD = new BigDecimal(adbl);
                BigDecimal bdblD = new BigDecimal(bdbl);
                BigDecimal cdblD = new BigDecimal(cdbl);
                BigDecimal ddblD = new BigDecimal(ddbl);
                if (StringUtils.equals(bosfgb, "0")) {
                    Map<String, String> map = checkData(1, billid, formtable, Integer.parseInt(site1aplusrs), Integer.parseInt(site1ars), Integer.parseInt(site1brs), Integer.parseInt(site1crs), Integer.parseInt(site1drs));
                    String sign = map.get("sign");
                    if (StringUtils.equals(sign, "1")) {
                        requestInfo.getRequestManager().setMessage(map.get("msg"));
                        return Action.FAILURE_AND_CONTINUE;
                    }
//                    handleData(1, billid, formtable, aplusdblD, adblD, bdblD, cdblD);
                    rs1.executeUpdate("UPDATE " + formtable + " SET bosfgb = 1 WHERE requestid = ?", requestId);
                }
                if (StringUtils.equals(opsfgb, "0")) {
                    Map<String, String> map = checkData(2, billid, formtable, Integer.parseInt(site2aplusrs), Integer.parseInt(site2ars), Integer.parseInt(site2brs), Integer.parseInt(site2crs), Integer.parseInt(site2drs));
                    String sign = map.get("sign");
                    if (StringUtils.equals(sign, "1")) {
                        requestInfo.getRequestManager().setMessage(map.get("msg"));
                        return Action.FAILURE_AND_CONTINUE;
                    }
//                    handleData(2, billid, formtable, aplusdblD, adblD, bdblD, cdblD);
                    rs1.executeUpdate("UPDATE " + formtable + " SET opsfgb = 1 WHERE requestid = ?", requestId);
                }
                if (StringUtils.equals(finsfgb, "0")) {
                    Map<String, String> map = checkData(3, billid, formtable, Integer.parseInt(site3aplusrs), Integer.parseInt(site3ars), Integer.parseInt(site3brs), Integer.parseInt(site3crs), Integer.parseInt(site3drs));                    String sign = map.get("sign");
                    if (StringUtils.equals(sign, "1")) {
                        requestInfo.getRequestManager().setMessage(map.get("msg"));
                        return Action.FAILURE_AND_CONTINUE;
                    }
//                    handleData(3, billid, formtable, aplusdblD, adblD, bdblD, cdblD);
                    rs1.executeUpdate("UPDATE " + formtable + " SET finsfgb = 1 WHERE requestid = ?", requestId);
                }
                if (StringUtils.equals(iadsfgb, "0")) {
                    Map<String, String> map = checkData(4, billid, formtable, Integer.parseInt(site4aplusrs), Integer.parseInt(site4ars), Integer.parseInt(site4brs), Integer.parseInt(site4crs), Integer.parseInt(site4drs));                    String sign = map.get("sign");
                    if (StringUtils.equals(sign, "1")) {
                        requestInfo.getRequestManager().setMessage(map.get("msg"));
                        return Action.FAILURE_AND_CONTINUE;
                    }
//                    handleData(4, billid, formtable, aplusdblD, adblD, bdblD, cdblD);
                    rs1.executeUpdate("UPDATE " + formtable + " SET iadsfgb = 1 WHERE requestid = ?", requestId);
                }
                if (StringUtils.equals(crisfgb, "0")) {
                    Map<String, String> map = checkData(5, billid, formtable, Integer.parseInt(site1aplusrs), Integer.parseInt(site5ars), Integer.parseInt(site5brs), Integer.parseInt(site5crs), Integer.parseInt(site5drs));                    String sign = map.get("sign");
                    if (StringUtils.equals(sign, "1")) {
                        requestInfo.getRequestManager().setMessage(map.get("msg"));
                        return Action.FAILURE_AND_CONTINUE;
                    }
//                    handleData(5, billid, formtable, aplusdblD, adblD, bdblD, cdblD);
                    rs1.executeUpdate("UPDATE " + formtable + " SET crisfgb = 1 WHERE requestid = ?", requestId);
                }
            }
        } catch (Exception e) {
            baseBean.writeLog("GradeCountAgainAction error:" + e.getMessage());
        }
        return Action.SUCCESS;
    }

    public Map<String, String> checkDataOld(int dth, int billid, String formtable, BigDecimal aplusdblD, String aplusqzbs, BigDecimal adblD,
                                         String aqzbs, BigDecimal bdblD, String bqzbs, BigDecimal cdblD, String cqzbs, BigDecimal ddblD, String dqzbs) {
        Map<String, String> resultMap = new HashMap<>();
        RecordSet rs = new RecordSet();
        rs.executeQuery("SELECT * FROM " + formtable + "_dt" + dth + " WHERE mainid = ?", billid);
        int allCount = 0;
        int aplusCount = 0;
        int aCount = 0;
        int bCount = 0;
        int cCount = 0;
        int dCount = 0;
        while (rs.next()) {
            String dj = rs.getString("dj");
            if (StringUtils.isEmpty(dj)) {
                resultMap.put("sign", "1");
                resultMap.put("msg", "存在员工等级未设置！");
                return resultMap;
            }
            allCount = allCount + 1;
            if (StringUtils.equals(dj, "0")) {
                aplusCount = aplusCount + 1;
            }
            if (StringUtils.equals(dj, "1")) {
                aCount = aCount + 1;
            }
            if (StringUtils.equals(dj, "2")) {
                bCount = bCount + 1;
            }
            if (StringUtils.equals(dj, "3")) {
                cCount = cCount + 1;
            }
            if (StringUtils.equals(dj, "4")) {
                dCount = dCount + 1;
            }
        }
        if (allCount <= 0) {
            resultMap.put("sign", "0");
            return resultMap;
        }
        int aplusdjSize = 0;
        if (StringUtils.equals(aplusqzbs, "0")) {
            aplusdjSize = aplusdblD.multiply(new BigDecimal(String.valueOf(allCount))).divide(new BigDecimal("100"), RoundingMode.UP).intValue();
        }
        if (StringUtils.equals(aplusqzbs, "1")) {
            aplusdjSize = aplusdblD.multiply(new BigDecimal(String.valueOf(allCount))).divide(new BigDecimal("100"), RoundingMode.DOWN).intValue();
        }
        if (aplusCount > aplusdjSize) {
            resultMap.put("sign", "1");
            resultMap.put("msg", "A+等级人数超额，请调整相关等级!");
            return resultMap;
        }
        int adjSize = 0;
        if (StringUtils.equals(aqzbs, "0")) {
            adjSize = adblD.multiply(new BigDecimal(String.valueOf(allCount))).divide(new BigDecimal("100"), RoundingMode.UP).intValue();
        }
        if (StringUtils.equals(aqzbs, "1")) {
            adjSize = adblD.multiply(new BigDecimal(String.valueOf(allCount))).divide(new BigDecimal("100"), RoundingMode.DOWN).intValue();
        }
        if (aCount > adjSize) {
            resultMap.put("sign", "1");
            resultMap.put("msg", "A等级人数超额，请调整相关等级!");
            return resultMap;
        }
        int bdjSize = 0;
        if (StringUtils.equals(bqzbs, "0")) {
            bdjSize = bdblD.multiply(new BigDecimal(String.valueOf(allCount))).divide(new BigDecimal("100"), RoundingMode.UP).intValue();
        }
        if (StringUtils.equals(bqzbs, "1")) {
            bdjSize = bdblD.multiply(new BigDecimal(String.valueOf(allCount))).divide(new BigDecimal("100"), RoundingMode.DOWN).intValue();
        }
        if (bCount > bdjSize) {
            resultMap.put("sign", "1");
            resultMap.put("msg", "B等级人数超额，请调整相关等级!");
            return resultMap;
        }
        int cdjSize = 0;
        if (StringUtils.equals(cqzbs, "0")) {
            cdjSize = cdblD.multiply(new BigDecimal(String.valueOf(allCount))).divide(new BigDecimal("100"), RoundingMode.UP).intValue();
        }
        if (StringUtils.equals(cqzbs, "1")) {
            cdjSize = cdblD.multiply(new BigDecimal(String.valueOf(allCount))).divide(new BigDecimal("100"), RoundingMode.DOWN).intValue();
        }
        if (cCount > cdjSize) {
            resultMap.put("sign", "1");
            resultMap.put("msg", "C等级人数超额，请调整相关等级!");
            return resultMap;
        }
        int ddjSize = 0;
        if (StringUtils.equals(dqzbs, "0")) {
            ddjSize = ddblD.multiply(new BigDecimal(String.valueOf(allCount))).divide(new BigDecimal("100"), RoundingMode.UP).intValue();
        }
        if (StringUtils.equals(dqzbs, "1")) {
            ddjSize = ddblD.multiply(new BigDecimal(String.valueOf(allCount))).divide(new BigDecimal("100"), RoundingMode.DOWN).intValue();
        }
        if (dCount > ddjSize) {
            resultMap.put("sign", "1");
            resultMap.put("msg", "D等级人数超额，请调整相关等级!");
            return resultMap;
        }
        resultMap.put("sign", "0");
        return resultMap;
    }

    public Map<String, String> checkData(int dth, int billid, String formtable, int aplusdjSize, int adjSize, int bdjSize, int cdjSize, int ddjSize) {
        Map<String, String> resultMap = new HashMap<>();
        RecordSet rs = new RecordSet();
        rs.executeQuery("SELECT * FROM " + formtable + "_dt" + dth + " WHERE mainid = ?", billid);
        int allCount = 0;
        int aplusCount = 0;
        int aCount = 0;
        int bCount = 0;
        int cCount = 0;
        int dCount = 0;
        while (rs.next()) {
            String dj = rs.getString("dj");
            if (StringUtils.isEmpty(dj)) {
                resultMap.put("sign", "1");
                resultMap.put("msg", "存在员工等级未设置！");
                return resultMap;
            }
            allCount = allCount + 1;
            if (StringUtils.equals(dj, "0")) {
                aplusCount = aplusCount + 1;
            }
            if (StringUtils.equals(dj, "1")) {
                aCount = aCount + 1;
            }
            if (StringUtils.equals(dj, "2")) {
                bCount = bCount + 1;
            }
            if (StringUtils.equals(dj, "3")) {
                cCount = cCount + 1;
            }
            if (StringUtils.equals(dj, "4")) {
                dCount = dCount + 1;
            }
        }
        if (allCount <= 0) {
            resultMap.put("sign", "0");
            return resultMap;
        }
        if (aplusCount > aplusdjSize) {
            resultMap.put("sign", "1");
            resultMap.put("msg", "A+等级人数超额，请调整相关等级!");
            return resultMap;
        }
        if (aCount > adjSize) {
            resultMap.put("sign", "1");
            resultMap.put("msg", "A等级人数超额，请调整相关等级!");
            return resultMap;
        }

        if (bCount > bdjSize) {
            resultMap.put("sign", "1");
            resultMap.put("msg", "B等级人数超额，请调整相关等级!");
            return resultMap;
        }

        if (cCount > cdjSize) {
            resultMap.put("sign", "1");
            resultMap.put("msg", "C等级人数超额，请调整相关等级!");
            return resultMap;
        }

        if (dCount > ddjSize) {
            resultMap.put("sign", "1");
            resultMap.put("msg", "D等级人数超额，请调整相关等级!");
            return resultMap;
        }
        resultMap.put("sign", "0");
        return resultMap;
    }

    public void handleData(int dth, int billid, String formtable, BigDecimal aplusdblD, BigDecimal adblD, BigDecimal bdblD, BigDecimal cdblD) {
        RecordSet rs = new RecordSet();
        rs.executeQuery("SELECT * FROM " + formtable + "_dt" + dth + " WHERE mainid = ?", billid);
        List<UfDjfbjgVo> ufDjfbjgVoList = new ArrayList<>();
        while (rs.next()) {
            UfDjfbjgVo ufDjfbjgVo = new UfDjfbjgVo();
            int id = rs.getInt("id");
            int xm = rs.getInt("xm");
            int bm = rs.getInt("bm");
            int jxkhjh = rs.getInt("jxkhjh");
            int ywx = rs.getInt("ywx");
            String fp2 = rs.getString("fp2");
            String ywmbm = rs.getString("ywmbm");
            String zp = rs.getString("zp");
            String fp1 = rs.getString("fp1");
            int dj = rs.getInt("dj");
            ufDjfbjgVo.setXm(xm);
            ufDjfbjgVo.setBm(bm);
            ufDjfbjgVo.setYwmbm(ywmbm);
            ufDjfbjgVo.setJxkhjh(jxkhjh);
            ufDjfbjgVo.setYwx(ywx);
            ufDjfbjgVo.setDj(dj);
            ufDjfbjgVo.setZp(zp);
            ufDjfbjgVo.setFp1(fp1);
            ufDjfbjgVo.setFp2(Double.parseDouble(fp2));
            ufDjfbjgVo.setId(id);
            ufDjfbjgVoList.add(ufDjfbjgVo);
        }
        if (CollectionUtils.isEmpty(ufDjfbjgVoList)) {
            return;
        }
        ufDjfbjgVoList = ufDjfbjgVoList.stream().sorted(Comparator.comparing(UfDjfbjgVo::getFp2).reversed())
                .collect(Collectors.toList());
        baseBean.writeLog("ufDjfbjgVoList A:" + JSONObject.toJSONString(ufDjfbjgVoList));
        String insertDetailSql = "INSERT INTO " + formtable + "_dt" + dth + " (mainid,xm,bm,ywmbm,ywx,jxkhjh,fp2,dj) " +
                "VALUES (?,?,?,?,?,?,?,?)";
        int size = ufDjfbjgVoList.size();
        int index = 0;
        Integer indexAPlus = aplusdblD.multiply(new BigDecimal(String.valueOf(size))).divide(new BigDecimal("100"), RoundingMode.UP).intValue();
        baseBean.writeLog("indexAPlus is:" + indexAPlus);
        for (int i = 0; i < indexAPlus; i++) {
            if (i >= size) {
                continue;
            }
            ufDjfbjgVoList.get(i).setDj(0);
        }
        baseBean.writeLog("ufDjfbjgVoList B:" + JSONObject.toJSONString(ufDjfbjgVoList) + "," + size);
        if (indexAPlus > size) {
            rs.execute("DELETE FROM "  + formtable + "_dt" + dth + " WHERE mainid = " + billid);
            List<List> insertParams = new ArrayList<>();
            for (UfDjfbjgVo ufDjfbjgVo : ufDjfbjgVoList) {
                List rowParam = new ArrayList<>();
                rowParam.add(billid);
                rowParam.add(ufDjfbjgVo.getXm());
                rowParam.add(ufDjfbjgVo.getBm());
                rowParam.add(ufDjfbjgVo.getYwmbm());
                rowParam.add(ufDjfbjgVo.getYwx());
                rowParam.add(ufDjfbjgVo.getJxkhjh());
                rowParam.add(ufDjfbjgVo.getFp2());
                rowParam.add(ufDjfbjgVo.getDj());
                insertParams.add(rowParam);
            }
            rs.executeBatchSql(insertDetailSql, insertParams);
            return;
        }
        Integer indexA = adblD.multiply(new BigDecimal(String.valueOf(size))).divide(new BigDecimal("100"), RoundingMode.UP).intValue();
        index = indexAPlus + indexA;
        for (int i = indexAPlus; i < index; i++) {
            if (i >= size) {
                continue;
            }
            ufDjfbjgVoList.get(i).setDj(1);
        }
        baseBean.writeLog("ufDjfbjgVoList C:" + JSONObject.toJSONString(ufDjfbjgVoList) + "," + index);
        if (index > size) {
            rs.execute("DELETE FROM "  + formtable + "_dt" + dth + " WHERE mainid = " + billid);
            List<List> insertParams = new ArrayList<>();
            for (UfDjfbjgVo ufDjfbjgVo : ufDjfbjgVoList) {
                List rowParam = new ArrayList<>();
                rowParam.add(billid);
                rowParam.add(ufDjfbjgVo.getXm());
                rowParam.add(ufDjfbjgVo.getBm());
                rowParam.add(ufDjfbjgVo.getYwmbm());
                rowParam.add(ufDjfbjgVo.getYwx());
                rowParam.add(ufDjfbjgVo.getJxkhjh());
                rowParam.add(ufDjfbjgVo.getFp2());
                rowParam.add(ufDjfbjgVo.getDj());
                insertParams.add(rowParam);
            }
            rs.executeBatchSql(insertDetailSql, insertParams);
            return;
        }
        Integer indexB = bdblD.multiply(new BigDecimal(String.valueOf(size))).divide(new BigDecimal("100"), RoundingMode.UP).intValue();
        int indexN = index + indexB;
        for (int i = index; i < indexN; i++) {
            if (i >= size) {
                continue;
            }
            ufDjfbjgVoList.get(i).setDj(2);
        }
        baseBean.writeLog("ufDjfbjgVoList D:" + JSONObject.toJSONString(ufDjfbjgVoList) + "," + indexN);
        if (indexN > size) {
            rs.execute("DELETE FROM "  + formtable + "_dt" + dth + " WHERE mainid = " + billid);
            List<List> insertParams = new ArrayList<>();
            for (UfDjfbjgVo ufDjfbjgVo : ufDjfbjgVoList) {
                List rowParam = new ArrayList<>();
                rowParam.add(billid);
                rowParam.add(ufDjfbjgVo.getXm());
                rowParam.add(ufDjfbjgVo.getBm());
                rowParam.add(ufDjfbjgVo.getYwmbm());
                rowParam.add(ufDjfbjgVo.getYwx());
                rowParam.add(ufDjfbjgVo.getJxkhjh());
                rowParam.add(ufDjfbjgVo.getFp2());
                rowParam.add(ufDjfbjgVo.getDj());
                insertParams.add(rowParam);
            }
            rs.executeBatchSql(insertDetailSql, insertParams);
            return;
        }
        Integer indexC = cdblD.multiply(new BigDecimal(String.valueOf(size))).divide(new BigDecimal("100"), RoundingMode.UP).intValue();
        int indexNN = indexN + indexC;
        for (int i = indexN; i < indexNN; i++) {
            if (i >= size) {
                continue;
            }
            ufDjfbjgVoList.get(i).setDj(3);
        }
        baseBean.writeLog("ufDjfbjgVoList e:" + JSONObject.toJSONString(ufDjfbjgVoList) + "," + indexNN);
        if (indexNN > size) {
            rs.execute("DELETE FROM "  + formtable + "_dt" + dth + " WHERE mainid = " + billid);
            List<List> insertParams = new ArrayList<>();
            for (UfDjfbjgVo ufDjfbjgVo : ufDjfbjgVoList) {
                List rowParam = new ArrayList<>();
                rowParam.add(billid);
                rowParam.add(ufDjfbjgVo.getXm());
                rowParam.add(ufDjfbjgVo.getBm());
                rowParam.add(ufDjfbjgVo.getYwmbm());
                rowParam.add(ufDjfbjgVo.getYwx());
                rowParam.add(ufDjfbjgVo.getJxkhjh());
                rowParam.add(ufDjfbjgVo.getFp2());
                rowParam.add(ufDjfbjgVo.getDj());
                insertParams.add(rowParam);
            }
            rs.executeBatchSql(insertDetailSql, insertParams);
            return;
        }
        for (int i = indexNN; i < size; i++) {
            ufDjfbjgVoList.get(i).setDj(4);
        }
        rs.execute("DELETE FROM "  + formtable + "_dt" + dth + " WHERE mainid = " + billid);
        List<List> insertParams = new ArrayList<>();
        for (UfDjfbjgVo ufDjfbjgVo : ufDjfbjgVoList) {
            List rowParam = new ArrayList<>();
            rowParam.add(billid);
            rowParam.add(ufDjfbjgVo.getXm());
            rowParam.add(ufDjfbjgVo.getBm());
            rowParam.add(ufDjfbjgVo.getYwmbm());
            rowParam.add(ufDjfbjgVo.getYwx());
            rowParam.add(ufDjfbjgVo.getJxkhjh());
            rowParam.add(ufDjfbjgVo.getFp2());
            rowParam.add(ufDjfbjgVo.getDj());
            insertParams.add(rowParam);
        }
        rs.executeBatchSql(insertDetailSql, insertParams);
    }
}
